package com.applications.codemash;

import com.applications.codemash.core.BaseActivity;
import com.applications.codemash.core.Constants;
import com.applications.codemash.core.Session;
import com.applications.codemash.core.Speaker;
import com.applications.codemash.core.eventhandlers.SessionNotifyCheckChangeListener;
import com.applications.codemash.utility.CustomContext;

import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.TextView;

public class SessionInfo extends BaseActivity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
    {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.session_info);
    	
    	Session session = (Session)getIntent().getExtras().get(Constants.SESSION_INTENT_KEY);
    	setOnScreenContent(session);
    }
	
	@Override
	public void onStart()
	{
		super.onStart();
		NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		Session session = (Session)getIntent().getExtras().get(Constants.SESSION_INTENT_KEY);
		manager.cancel(session.getNotificationId());
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.session_info_menu, menu);
	    return true;
    }
	
	 @Override
	 public boolean onOptionsItemSelected(MenuItem item)
	 {
		 Intent myIntent;
		 Session session = (Session)getIntent().getExtras().get(Constants.SESSION_INTENT_KEY);
		 
		 switch (item.getItemId())
		 {
			 case R.id.menuSessionInfoSpeakerInfo:
				 myIntent = new Intent(getBaseContext(), SpeakerInfo.class);
				 Speaker speaker = CustomContext.getSpeakerCache().getSpeakerByUri(session.getSpeakerUri());
				 myIntent.putExtra(Constants.SPEAKER_INTENT_KEY, speaker);
				 startActivity(myIntent);
				 break;
			 case R.id.menuSessionInfoTrackRelated:
				 myIntent = new Intent(getBaseContext(), TrackInfo.class);
				 myIntent.putExtra(Constants.SESSION_TRACK_INTENT_KEY, session.getTrack());
				 startActivity(myIntent);
				 break;
		 }
		 return true;
	 }
	 
	 private void setOnScreenContent(Session session)
	 {
		 setTitle("CodeMash: " + session.getTitle());
		 ((TextView)findViewById(R.id.txtSessionTitle)).setText(session.getTitle());
		 ((TextView)findViewById(R.id.txtSpeaker)).setText(session.getSpeakerName());
	    	
		 ((TextView)findViewById(R.id.txtSessionAbstract)).setText(Html.fromHtml(session.getAbstract()));
		 ((TextView)findViewById(R.id.txtSessionRoom)).setText("Room: " + session.getRoom());
		 ((TextView)findViewById(R.id.txtSessionTime)).setText("Start: " + session.getStartDisplay());
		 ((TextView)findViewById(R.id.txtSessionDifficulty)).setText("Difficulty: " + session.getDifficulty());
		 ((TextView)findViewById(R.id.txtTechnology)).setText("Tech: " + session.getTechnology());
		 ((TextView)findViewById(R.id.txtTrack)).setText("Track: " + session.getTrack());
		 
		 ((CheckBox)findViewById(R.id.chkNotify)).setChecked(session.getNotifyOnStart());
		 ((CheckBox)findViewById(R.id.chkNotify)).setOnCheckedChangeListener(
				 new SessionNotifyCheckChangeListener(session.getSessionUri(), this));
	 }
}
